using System;
using System.Collections;
using System.Data;

namespace LocalBoard.Persistence.MockAccessor
{
	/// <summary>
	/// Summary description for MockClubCategoryAccessor.
	/// </summary>
	public class MockClubCategoryAccessor : IClubCategoryAccessor, IMockAccessor
	{
		private int _oId;
		private DataTable _clubCategoryTable = new DataTable("ClubCategory");
		private MockAccessorFactory _accessorFactory = new MockAccessorFactory();

		public static readonly MockClubCategoryAccessor Instance = new MockClubCategoryAccessor();

		private MockClubCategoryAccessor()
		{
			_clubCategoryTable.Columns.Add("Id", typeof(int));
			_clubCategoryTable.Columns.Add("CategoryName", typeof(string));
			Initialize();
		}

		private void Initialize()
		{
			_oId = 0;
			Insert("Administrative");
		}

		#region IMockAccessor
		public void ResetDataTable()
		{
			_clubCategoryTable.Clear();
			Initialize();
		}

		public void RemoveRows(int tableIndex, DataRow[] rows)
		{
			throw new NotImplementedException();
		}

		public DataRow[] ExecuteFilter(string filterExpression)
		{
			throw new NotImplementedException();
		}
		#endregion

		#region IClubCategoryAccessor
		/// <summary>
		/// Delete club category
		/// </summary>
		/// <param name="id">Club category id</param>
		public void Delete(int id)
		{
			DataRow row = FindClubCategoryDataRow(id);
			if(row!=null)
			{
				_clubCategoryTable.Rows.Remove(row);
				_clubCategoryTable.AcceptChanges();
			}
		}

		/// <summary>
		/// Get all club categories
		/// </summary>
		/// <returns>Hashtable of all categories with category id as key and category name as value</returns>
		public Hashtable GetAllClubCategories()
		{
			Hashtable clubCategories = new Hashtable();
			foreach(DataRow row in _clubCategoryTable.Rows)
			{
				clubCategories.Add(DataConverter.GetInteger(row["id"]), DataConverter.GetString(row["CategoryName"]));
			}
			return clubCategories;
		}

		/// <summary>
		/// Insert club category
		/// </summary>
		/// <param name="clubCategoryName">Club category name</param>
		/// <returns>Club category id</returns>
		public int Insert(string clubCategoryName)
		{
			_oId++;
			DataRow newRow = _clubCategoryTable.NewRow();
			newRow["Id"] = _oId;
			newRow["CategoryName"] = clubCategoryName;
			_clubCategoryTable.Rows.Add(newRow);

			return _oId;
		}

		/// <summary>
		/// Update club category
		/// </summary>
		/// <param name="clubCategoryName">New club category</param>
		/// <param name="id">Club category id to change</param>
		public void Update(string clubCategoryName, int id)
		{
			DataRow row = FindClubCategoryDataRow(id);
			if(row!=null)
			{
				row["CategoryName"] = clubCategoryName;
				_clubCategoryTable.AcceptChanges();
			}
		}
		#endregion

		private DataRow FindClubCategoryDataRow(int id)
		{
			foreach(DataRow row in _clubCategoryTable.Rows)
			{
				if(DataConverter.GetInteger(row["Id"])==id)
					return row;
			}
			return null;
		}
	}
}
